package com.pubinfo.passbook.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TSystemUser;
import com.pubinfo.passbook.common.model.dto.financial.InvoiceResult;
import com.pubinfo.passbook.common.model.dto.financial.QueryDetailsParam;
import com.pubinfo.passbook.common.model.dto.financial.Result;
import com.pubinfo.passbook.common.service.master.TPassbookService;
import com.pubinfo.passbook.common.service.master.TSystemUserService;
import com.pubinfo.passbook.common.service.request.financial.RequestFinancialServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "com/pubinfo/passbook")
@Api(tags = "增值税系统调用")
@Slf4j
public class VATController {
  
  @Autowired
  TPassbookService passbookService;
  @Autowired
  RequestFinancialServer financialServer;
  
  @Resource
  TSystemUserService userService;
  
  @PostMapping("detailsByParam")
  @ApiOperation(value = "根据参数查询详情")
  public InvoiceResult<Result> detailsByParam(@RequestBody @Valid QueryDetailsParam detailsParam, HttpServletResponse response) throws IOException, UnirestException {
    String username = detailsParam.getUsername();
    String password = detailsParam.getPassword();
    
    if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
      return new InvoiceResult<>("401", "请输入认证数据");
    }
    QueryWrapper<TSystemUser> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username", username);
    List<TSystemUser> userList = userService.list(queryWrapper);
    if (CollectionUtils.isEmpty(userList)) {
      return new InvoiceResult<>("401", "该用户并不存在!");
    }
    TSystemUser user = userList.get(0);
    //密码校验
    //  if (Pbkdf2Sha256.verificationHash(Base64.getDecoder().decode(password), user.getPassword(), user.getSalt())) {
    return financialServer.detailsByParam(detailsParam);
    // }
    // return new InvoiceResult<>();
  }

//  @GetMapping("test/list")
//  @ApiOperation(value = "测试环境列表数据")
//  public JsonResult<Page<TPassbook>> list(@RequestBody PassbookPageParamObj base) {
//    return JsonResult.success(new Page(passbookService.listTPassbook(base)));
//  }
}
